<?php

namespace app\modules\api\controllers;

use app\models\Agent;
use app\models\ApiException;
use app\models\Card;
use app\models\CardSms;
use app\models\User;
use yii\data\Pagination;

/**
 * 短信接口
 * Class SmsController
 * @package app\modules\api\controllers
 */
class SmsController extends BaseController
{
    /**
     * 依次尝试根据id ICCID MSISDN IMSI获取卡
     * @param Agent|User $user 登录代理商或用户
     * @param string $idName 卡编号的参数名
     * @return Card
     * @throws ApiException
     */
    private function getCard($user = null, $idName = 'card_id')
    {
        $id = $this->json($idName);
        $iccid = $this->json('iccid');
        $msisdn = $this->json('msisdn');
        $imsi = $this->json('imsi');

        $card = null;
        if (!empty($id)) {
            $card = Card::findOne(['id' => $id]);
        } elseif (!empty($iccid)) {
            $card = Card::findOne(['iccid' => $iccid]);
        } elseif (!empty($msisdn)) {
            $card = Card::findOne(['msisdn' => $msisdn]);
        } elseif (!empty($imsi)) {
            $card = Card::findOne(['imsi' => $imsi]);
        }

        if (!empty($user)) {
            if (($user instanceof Agent) && $card->agent_id != $user->id) {
                throw new ApiException('NOT_FOUND', '没有找到卡信息。');
            }
            if (($user instanceof User) && $card->uid != $user->id) {
                throw new ApiException('NOT_FOUND', '没有找到卡信息。');
            }
        }

        return $card;
    }

    /**
     * 发送短信
     * 输入
     * {
     *     msisdn,
     *     content
     * }
     * 输出
     * {}
     * @throws ApiException
     */
    public function actionSend()
    {
        $user = $this->loginUser('user');

        $card = $this->getCard($user);
        $content = $this->json('content');
        if (empty($content)) {
            throw new ApiException('PARAM', '没有找到短信内容。');
        }
        CardSms::send($card, $content);
        return [];
    }

    /**
     * 短信列表
     * 输入
     * {
     *     card_id,
     *     msisdn
     * }
     * 输出
     * {
     *     sms_list: [
     *         {id, card_id, type, time, content, status, info},
     *         ...
     *     ],
     *     page: {}
     * }
     * @throws ApiException
     */
    public function actionList()
    {
        $user = $this->loginUser('user');

        $card = $this->getCard($user);

        $query = CardSms::find();
        $query->andWhere(['card_id' => $card->id]);
        $pagination = new Pagination(['totalCount' => $query->count(), 'page' => $this->json('page', 1) - 1, 'validatePage' => false]);
        $query->orderBy('id DESC');
        $query->offset($pagination->offset)->limit($pagination->limit);
        $smsList = [];
        /** @var CardSms $sms */
        foreach ($query->each() as $sms) {
            $smsList[] = $sms->getAttributes(['id', 'card_id', 'type', 'time', 'content', 'status', 'info']);
        }
        return [
            'sms_list' => $smsList,
            'page' => [
                'totalCount' => $pagination->totalCount,
                'pageCount' => $pagination->pageCount,
                'page' => $pagination->page + 1,
            ],
        ];
    }
}
